﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IDataRelation))]
    [Guid("E5453BDB-5A1E-433C-9ACF-FD9825093705")]
    public class DataRelation : IDataRelation
    {
        private AdoNet.DataRelation _adoNetRelation;

        public DataRelation() : this(null)
        {}

        public DataRelation(AdoNet.DataRelation adoNetRelation)
        {
            if (adoNetRelation == null)
                throw new ArgumentNullException("adoNetRelation");

            _adoNetRelation = adoNetRelation;
        }

        [ComVisible(false)]
        public AdoNet.DataRelation AdoNetDataRelation
        {
            get { return _adoNetRelation; }
        }

        public virtual DataColumnList ChildColumns 
        {
            get 
            {
                DataColumn[] result = (from column in _adoNetRelation.ChildColumns
                                       select new DataColumn(column)).ToArray();

                return new DataColumnList(result);
            }
        }

        public virtual ForeignKeyConstraint ChildKeyConstraint
        {
            get
            {
                return new ForeignKeyConstraint(_adoNetRelation.ChildKeyConstraint);
            }
        }
        
        public virtual DataTable ChildTable 
        {
            get { return new DataTable(_adoNetRelation.ChildTable); }
        }

        public virtual DataSet DataSet
        {
            get { return new DataSet(_adoNetRelation.DataSet); }
        }
                
        public virtual bool Nested 
        {
            get { return _adoNetRelation.Nested; }
            set { _adoNetRelation.Nested = value; }
        }
        
        public virtual DataColumnList ParentColumns 
        {
            get
            {
                DataColumn[] result = (from column in _adoNetRelation.ParentColumns
                                       select new DataColumn(column)).ToArray();

                return new DataColumnList(result);
            }
        }
        
        public virtual UniqueConstraint ParentKeyConstraint 
        {
            get { return new UniqueConstraint(_adoNetRelation.ParentKeyConstraint); }
        }
        
        public virtual DataTable ParentTable 
        {
            get { return new DataTable(_adoNetRelation.ParentTable); } 
        }
        
        public virtual string RelationName 
        {
            get { return _adoNetRelation.RelationName; }
            set { _adoNetRelation.RelationName = value;  }
        }

        public override string ToString()
        {
            return _adoNetRelation.ToString();
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("154B6D23-2304-4349-AE70-FDB06FB4A512")]
    public interface IDataRelation
    {
        DataColumnList ChildColumns { get; }

        ForeignKeyConstraint ChildKeyConstraint { get; }

        DataTable ChildTable { get; }

        DataSet DataSet { get; }

        bool Nested { get; set; }

        DataColumnList ParentColumns { get; }

        UniqueConstraint ParentKeyConstraint { get; }

        DataTable ParentTable { get; }

        string RelationName { get; set; }

        string ToString();
    }
}
